﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BillingSystem
{
    /// <summary>Диалог редактирования позиции тарифной сетки</summary>
    public partial class FormEditRateScheduleItem : Form, IDialog
    {
        /// <summary>Это так надо.</summary>
        private BindingSource directionsBindingSource;
        private decimal tariff_id;
        
        public FormEditRateScheduleItem(BindingSource directionsBindingSource, decimal tariff_id)
        {
            InitializeComponent();
            this.directionsBindingSource = directionsBindingSource;
            this.tariff_id = tariff_id;
        }

        /// <summary> Показ диалога </summary>
        /// <param name="bs"></param>
        /// <returns></returns>
        public bool ShowForm(BindingSource bs)
        {
            DataRowView drv = (DataRowView)bs.Current;

            if (drv.IsNew)
            {
                drv["DIRECTION_ID"] = 1;
                drv["TARIFF_ID"] = tariff_id;
                drv["PRICE"] = 0;
                this.Text = "";
            }
            else
            {
                this.Text = "Редактирование направления";
            }

            comboBoxDirection.DataSource = this.directionsBindingSource;
            comboBoxDirection.DisplayMember = "NAME";
            comboBoxDirection.ValueMember = "DIRECTION_ID";
            comboBoxDirection.DataBindings.Add("SelectedValue", drv, "DIRECTION_ID", false,
                DataSourceUpdateMode.OnValidation);

            textBoxCost.DataBindings.Add("Text", drv, "PRICE", false,
                DataSourceUpdateMode.OnValidation, null, "N0");

            if (this.ShowDialog() == DialogResult.OK)
            {
                if (textBoxCost.Text == null)
                {
                    drv.CancelEdit();
                    return false;
                }
                drv.EndEdit();
                return true;
            }
            else
            {
                drv.CancelEdit();
                return false;
            }
        }
    }
}
